package com.tiantianfresh.admin.controller;

import com.tiantianfresh.model.pojo.AJAXResult;
import com.tiantianfresh.model.pojo.Permission;
import com.tiantianfresh.admin.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author FANGAO
 * @date 2021/11/14 - 下午 18:04
 */
@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @RequestMapping("/index")
    public String index(){

        return "permission/index";
    }

    @ResponseBody
    @RequestMapping("/loadData")
    public Object loadData(){
        List<Permission> permissionList = new ArrayList<Permission>();
        List<Permission> ps = permissionService.queryAll();
        Map<Integer,Permission> map = new HashMap<Integer,Permission>();
        for (Permission p : ps) {
            map.put(p.getId(),p);
        }
        for (Permission p : ps){
            Permission child = p;
            if (p.getPid()==0){
                permissionList.add(p);
            }else{
                //如果get到了值就说明找到了他的父节点，就把拿到的值放到父节点中
                Permission parent = map.get(p.getPid());
                parent.getChildren().add(child);
            }
        }
        return permissionList;
    }

    /**
     * 递归查询许可信息
     * 1.方法自己调自己
     * 2.方法一定要存在跳出逻辑
     * 3.方法调用时，参数之间应存在规律
     * 4.此方法效率低每循环一次就要访问一次数据库
     * @param parent
     */
    private void queryChidPermissions(Permission parent){
        //拿到符合pid的角色
        List<Permission> chidPermissions = permissionService.queryChidPermissions(parent.getId());
        //循环遍历将此分支中的每个子分支拿出来，通过递归拿到他的子分支下所有的子分支
        for (Permission permission : chidPermissions) {
            queryChidPermissions(permission);
        }
        //将每次拿到的子分支存到他的父分支中
        parent.setChildren(chidPermissions);
    }

    @RequestMapping("/add")
    public String add(){
        return "permission/add";
    }
    @ResponseBody
    @RequestMapping("/insert")
    public Object insert(Permission permission){
        AJAXResult result = new AJAXResult();
        try {
            result.setSuccess(permissionService.insert(permission));
        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }

    @RequestMapping("/edit")
    public String edit(Integer id,Model model){
        System.out.println(id);
        Permission permission = permissionService.queryById(id);
        model.addAttribute("permission",permission);
        return "permission/edit";
    }

    @ResponseBody
    @RequestMapping("/update")
    public Object update(Permission permission){
        AJAXResult result = new AJAXResult();
        try {
            result.setSuccess(permissionService.update(permission));
        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("/detele")
    public Object detele(Integer id){
        AJAXResult result = new AJAXResult();
        try {
            result.setSuccess(permissionService.detele(id));
        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("/loadAssingnData/{id}")
    public Object loadAssingnData(@PathVariable Integer id){
        List<Permission> permissionList = new ArrayList<Permission>();
        List<Permission> ps = permissionService.queryAll();
        //查询角色与许可的关系
        List<Integer> rolePermissions = permissionService.queryRolePermissions(id);

        Map<Integer,Permission> map = new HashMap<Integer,Permission>();
        for (Permission p : ps) {
            map.put(p.getId(),p);
        }
        for (Permission p : ps){
            //如果在角色与许可的关系表中包含该角色id，
            // 就说明此角色已经添加了该许可，
            // 就将Checked属性改为true勾上选项
            if (rolePermissions.contains(p.getId())){
                p.setChecked(true);
            }
            Permission child = p;
            if (p.getPid()==0){
                permissionList.add(p);
            }else{
                //如果get到了值就说明找到了他的父节点，就把拿到的值放到父节点中
                Permission parent = map.get(p.getPid());
                parent.getChildren().add(child);
            }
        }
        return permissionList;
    }
}
